<?
namespace Admin\Model
{
    /**
     * Manage realisation 
     */
     
    class Realisation extends \Api\Model\DB2
    {
        /**
         * Add realisation
         *
         * @param array $realisation - realisation data
         * @return bool
         */
        public function addRealisation(array $realisation)
        {
            $Q = $this->db->query("SHOW TABLE STATUS LIKE 'arch_image_group'");
            $res = $Q->fetch();
                        
            $Q = $this->db->prepare("INSERT INTO `arch_image_group` 
                VALUES(0,:desc,:name,'R')");
                
            $Q->bindValue(":desc",$realisation['desc'],\PDO::PARAM_STR);
            $Q->bindValue(":name",$realisation['name'],\PDO::PARAM_STR);
            $Q->execute();
                
            $Q = $this->db->prepare("INSERT INTO `arch_thumb` VALUES(0, :sum, :group)");
            $Q->bindValue(":sum",$realisation['img'],\PDO::PARAM_STR);
            $Q->bindValue(":group",$res['Auto_increment'],\PDO::PARAM_INT);
            $Q->execute();
        }
        
        /**
         * Dodaje zdjęcia do istniejącej realizacji
         * @param int $rid  - identyfikator group
         * @param array $images - lista z nazwami plików
         * @return bool
         * 
         */
       public function AddImage($rid, array $images)
       {
            foreach($images as $k => $v)
            {
                if(\Api\Image::MoveWithResize($v, \Api\Image::PIC))
                {
                    $Q = $this->db->prepare("INSERT INTO `arch_image` VALUES(0, :file, :group)");
                    $Q->bindValue(':file',$v,\PDO::PARAM_STR);
                    $Q->bindValue(':group',$rid,\PDO::PARAM_INT);
                    $Q->execute();
                }
            }
       }
        
        /**
         * Return realisation list
         *
         * @return array with realisation
         */
        public function GetRealisations()
        {
            $Q = $this->db->query("SELECT a.*, SUBSTR(a.name,0,25) AS `name`, b.name as file FROM arch_image_group as a LEFT JOIN arch_thumb as b ON a.id=b.group_id WHERE a.type='R';");
            return $Q->fetchAll();
        }
        
        /**
         * Return selected realisation data with thumb and images
         *
         * @param int $rid - realisation id
         * @return array
         */
        public function GetRealisation($rid)
        {
            $Q = $this->db->prepare("SELECT a.*, b.name as file FROM arch_image_group as a LEFT JOIN arch_thumb as b ON a.id=b.group_id WHERE a.type='R' AND a.id=:rid;");
            $Q->bindValue(':rid', $rid, \PDO::PARAM_INT);
            $Q->execute();
            
            if( false !== ( $res = $Q->fetch() ))
            {
                $Q = $this->db->prepare("SELECT * FROM `arch_image` WHERE `group_id`=:rid");
                $Q->bindValue(':rid', $rid, \PDO::PARAM_INT);
                $Q->execute();
                
                $res['image']=array();
                
                if( false !== ( $img = $Q->fetchAll() ))
                {
                    $res['image']=$img;
                }
                
                return $res;
            }
            return array();
        }
        
        /**
         * Remove realisation from db
         *
         * @param int $id - realisation identificator 
         * @return bool
         */
        public function RemoveRealisation($id)
        {

         // remove thumb
            $Q = $this->db->prepare("SELECT name as file FROM arch_thumb WHERE group_id = :id");
            $Q->bindValue(':id',$id,\PDO::PARAM_INT);                       
            $Q->execute();
            
            if( ($res = $Q->fetchAll()) !== false)
            {                
                foreach($res as $k => $v)
                {
                    @unlink(BASE_PATH.'/..'.\Api\Image::THUMB.'/'.$v['file']);
                }
            }
            
        // remove images
            $Q = $this->db->prepare("SELECT name as file FROM arch_image WHERE group_id = :id");
            $Q->bindValue(':id',$id,\PDO::PARAM_INT);                       
            $Q->execute();
            
            if( ($res = $Q->fetchAll()) !== false)
            {
                foreach($res as $k => $v)
                {
                   @unlink(BASE_PATH.'/..'.\Api\Image::PIC.'/'.$v['file']);
                }
            }
       
        // remove from db
            $Q = $this->db->prepare("DELETE FROM `arch_image_group` WHERE `id`=:id");
            $Q->bindValue(':id',$id,\PDO::PARAM_INT);                       
            
            return ($Q->execute() !==false);
        }
        
        /**
         * Aktualizacja danych opisu realizacji
         */
        public function UpdateDescription($id, $desc)
        {
            $Q = $this->db->prepare("UPDATE `arch_image_group` SET `desc`=:desc WHERE `id`=:id");
            $Q->bindValue(':id',$id,\PDO::PARAM_INT);
            $Q->bindValue(':desc',$desc,\PDO::PARAM_STR);
            $Q->execute();
        }
        
        /**
         * Usuwa zdjecie powiazane z realizacja o podanym identyfikatorze
         * @param int $rid - identyfikator realizacji
         * @param array $del - lista nazw plikow do usuniecia
         */
        public function RemoveImage($rid, array $images)
        {
            foreach($images as $k => $v)
            {
                $Q = $this->db->prepare("DELETE FROM `arch_image` WHERE `name`=:name AND `group_id`=:rid");
                $Q->bindValue(':rid',$rid,\PDO::PARAM_INT);
                $Q->bindValue(':name',$v,\PDO::PARAM_INT);
                $Q->execute();
                
                @unlink(BASE_PATH.'/..'.\Api\Image::PIC.'/'.$v);
            }
        }
         
    }
}
?>